<?php
namespace app\common\service;

use think\Model;

class FirstSubscribeService extends BaseService
{

    public function getDetail($sid){
        $detail = db('first')->where(['sid' => $sid])->find();
        if(empty($detail)){
            return false;
        }

        //获取图文信息
        if($detail['type'] == 2 || $detail['type'] == 3){
            $res = db('res')->where(['res_id' => $detail['res_id']])->find();

            $res_temp = $res['res'];
            $res_temp = str_replace(PHP_EOL, "", $res_temp);
            $temp = json_decode($res_temp, true);
            $detail['content'] = [
                'res_id' => $res['res_id'],
                'title' => $temp['title'],
                'create_time' => date('Y-m-d H:i:s', $res['create_time']),
                'coverimg' => $temp['coverimg'],
                'summary' => $temp['summary'] ?? '',
                'type' => $res['type'],
                'dataset' => $temp['dataset'] ?? ''
            ];
        }
        return ['content' => $detail];
    }

    /**
     * @param array $sid
     * @param int $first_id
     * @param array $postParam
     * @return int|string
     * @throws \think\Exception
     * 新增或更新首次关注内容
     */
    public function saveDetail($sid, $first_id = 0, $postParam = []){
        $data = [];
        $data['type'] = $postParam['type'];
        $data['content'] = $postParam['type'] == 1 ? $postParam['content'] : '';
        $data['res_id'] = $postParam['type'] == 1 ? 0 : $postParam['res_id'];
        if($first_id == 0){
            //新增
            $data['sid'] = $sid;
            return db('first')->insert($data);
        } else {
            //更新
            return db('first')->where(['sid' => $sid, 'first_id' => $first_id])->update($data);
        }

    }
}







